
package com.shop.cloud.weixin.admin.controller;

import cn.binarywang.wx.miniapp.bean.WxMaCodeLineColor;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.security.annotation.Inside;
import com.shop.cloud.weixin.admin.config.ma.WxMaConfiguration;
import com.shop.cloud.weixin.common.constant.WxReturnCode;
import com.shop.cloud.weixin.common.dto.MaQrCodeDTO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.io.FileInputStream;
import java.util.Base64;
import java.util.Map;

/**
 * 小程序码
 *
 * @author 
 * @date 2019-10-25 15:39:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmaqrcode")
@Tag(description = "wxmaqrcode", name = "小程序码API")
public class WxMaQrCodeController {

	/**
	 * 生成小程序码
	 * @param maQrCodeDTO
	 * @return
	 */
	@Operation(summary = "生成小程序码")
	@PostMapping("/unlimited")
	public R getUnlimited(@RequestBody MaQrCodeDTO maQrCodeDTO){
		try {
			Map<String, String> lineColor = maQrCodeDTO.getLineColor();
			WxMaCodeLineColor wxMaCodeLineColor = null;
			if(lineColor != null){
				wxMaCodeLineColor = new WxMaCodeLineColor(lineColor.get("r"), lineColor.get("g") ,lineColor.get("b"));
			}
			String wxAppId = maQrCodeDTO.getAppId();
			File fileImg = WxMaConfiguration.getMaService(wxAppId).getQrcodeService()
					.createWxaCodeUnlimit(maQrCodeDTO.getScene(), maQrCodeDTO.getPage(), true, null ,
							maQrCodeDTO.getWidth(), maQrCodeDTO.isAutoColor(), wxMaCodeLineColor, maQrCodeDTO.isHyaline());
			FileInputStream inputFile = new FileInputStream(fileImg);
			byte[] buffer = new byte[(int)fileImg.length()];
			inputFile.read(buffer);
			inputFile.close();
			String rs = new String(Base64.getEncoder().encode(buffer));
			return R.ok(rs);
		} catch (WxErrorException e) {
			e.printStackTrace();
			return WxReturnCode.wxErrorExceptionHandler(e);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}

	/**
	 * 生成小程序码（服务间调用）
	 * @param maQrCodeDTO
	 * @return
	 */
	@Inside
	@Operation(summary = "生成小程序码")
	@PostMapping("/inside/unlimited")
	public R getUnlimitedInside(@RequestBody MaQrCodeDTO maQrCodeDTO){
		try {
			String wxAppId = maQrCodeDTO.getAppId();
			File fileImg = WxMaConfiguration.getMaService(wxAppId).getQrcodeService()
					.createWxaCodeUnlimit(maQrCodeDTO.getScene(), maQrCodeDTO.getPage(),true, null ,100, true, null, false);
			FileInputStream inputFile = new FileInputStream(fileImg);
			byte[] buffer = new byte[(int)fileImg.length()];
			inputFile.read(buffer);
			inputFile.close();
			String rs = new String(Base64.getEncoder().encode(buffer));
			return R.ok(rs);
		} catch (WxErrorException e) {
			e.printStackTrace();
			return WxReturnCode.wxErrorExceptionHandler(e);
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}
}
